	$(function() {
		$("#createStrategy").dialog({
			autoOpen: false,
			width: 600,
			modal: true,
			resizable: false,
			buttons: {
				"添加策略": function() { 
				   var levelTag = $("#levelTag").val();
				   if(checkFiledName("create") & checkStrategy("create")) {
				   		createStragegy(levelTag, 'create');
				   		clearStragegy("create");
				   		$(this).dialog("close"); 
				   }
				   
				}, 
				" 取消 ": function() { 
				    clearStragegy("create");
					$(this).dialog("close"); 
				}
			}
		});
		
		$("#updateStrategy").dialog({
			autoOpen: false,
			width: 600,
			modal: true,
			resizable: false,
			buttons: {
				"修改策略": function() { 
				   var levelTag = $("#levelTag").val();
				   if(checkFiledName("update") & checkStrategy("update")) {
				   		createStragegy(levelTag, 'update');
				   		clearStragegy("update");
				   		$(this).dialog("close"); 
				   }
				}, 
				" 取消 ": function() { 
				    clearStragegy("update");
					$(this).dialog("close"); 
				}
			}
		});
	});
	
	function showNextStragegy() {
	  var levelObj = $("#level option:selected").val();
	  
	  if(levelObj == "NEXT") {
	     $("#next").show();
	  } else {
	     $("#next").hide();
	  }
	}
	
	function clearStragegy(opt) {
		var name = $("#"+opt+"FiledName").val("");
		var strategy = $("#"+opt+"Strategy option:selected").val();
		
		if(strategy == "TAG"){
			$("#"+opt+"StartTag").val("");
			$("#"+opt+"EndTag").val("");
		} else if(strategy == "REGULAR") {
			$("#"+opt+"Regular").val("");
		} else if(strategy == "XPATH") {
		 	$("#"+opt+"Xpath").val("");
		 	$("#"+opt+"XpathMode").val("");
		}
		$("#"+opt+"Strategy option[value='']").attr("selected",true);
		$("."+opt+"FilterTag").removeAttr("checked");
		var tagDiv = $("#"+opt+"TagDiv");
		var regularDiv = $("#"+opt+"RegularDiv");
		var xpathDiv = $("#"+opt+"XpathDiv");
		tagDiv.hide();
		regularDiv.hide();
		xpathDiv.hide();
	}
	
	function createStragegy(levelTag, opt) {
		var name = $("#"+opt+"FiledName").val();
		var strategy = $("#"+opt+"Strategy option:selected").val();
		var strategyParamValues="";
		var trObj = "";
		var listObj = $("#"+levelTag+"StrategyList");
		var inputName = "";
	
		var lastTr = listObj.find("tr:last");
		var lastTrId = "";
		if(opt == "update") {
			lastTrId = $("#currentTrId").val();
		} else {
			lastTrId = lastTr.attr("id");
		}
		var indexs = lastTrId.split("_");
		if(indexs.length == 2) {
			  var index = indexs[1];
			  if(opt == "update") {
					index = parseInt(index);
				} else {
					index = parseInt(index) + 1;
				}
			  
			  inputName = "collectionStrategies["+index+"]";
			  trObj = "<tr id='"+levelTag+"Index_"+index+"'>";
		 }
		
		
		trObj += "<td class='tdFiledName'><input type='hidden' name='"+inputName+".filedName' value='"+name+"'/><span>"+name+"</span></td><td class='tdStrategy'><input type='hidden' name='"+inputName+".strategy' value='"+strategy+"'/><span>"+strategy+"</span></td>";
		
		if(strategy == "TAG"){
			var startTag = $("#"+opt+"StartTag").val();
			var endTag = $("#"+opt+"EndTag").val();
			
			trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.startTag' value='"+startTag+"'/><input type='hidden' name='"+inputName+".strategyValue.endTag' value='"+endTag+"'/><span>"+startTag+","+endTag+"</span></td>";
		} else if(strategy == "REGULAR") {
			var regular = $("#"+opt+"Regular").val();
			trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.regular' value='"+regular+"'/><span>"+regular+"</span></td>";
		} else if(strategy == "XPATH") {
		 	var xpath = $("#"+opt+"Xpath").val();
		 	var xpathMode = $("#"+opt+"XpathMode").val();
		 	
		 	trObj += "<td class='tdStrategyValue'><input type='hidden' name='"+inputName+".strategyValue.xpath' value='"+xpath+"'/>";
		 	if($.trim(xpathMode) != "") {
		 		trObj += "<input type='hidden' name='"+inputName+".strategyValue.xpathMode' value='"+xpathMode+"'/><span>"+xpath+","+xpathMode+"</span></td>";
		 	} else {
		 		trObj += "<span>"+xpath+"</span></td>";
		 	}
		 	
		}
		
		var filterTags = $("."+opt+"FilterTag");
		trObj += "<td class='tdFilterTag'>"
		var filterTagValues = "";
		var filterTagInput = "";
		var filterTagText = "";
		filterTags.each(function(i) {
			var filterTag = $(this);
			if(filterTag.attr("checked")) {
				filterTagInput += "<input type='hidden' name='"+inputName+".filterTag["+i+"].tagName' value='"+filterTag.val()+"'/>";
				filterTagText += filterTag.val()+",";
			}
		});
		var lastPosition = filterTagText.lastIndexOf(",");
		if(lastPosition != -1) {
			filterTagText = filterTagText.substring(0, lastPosition);
		}
		trObj += filterTagInput+"<span>"+filterTagText+"</span></td><td><a href='javascript:updateStrategy(\""+levelTag+"Index_"+index+"\",\""+levelTag+"\")'>修改</a>|<a href='javascript:deleteStrategy(\""+levelTag+"Index_"+index+"\")' onclick='return confirm(\"您真的要做删除操作？\")'>删除</a></td></tr>";
		
		if(opt == "update") {
			lastTrId = $("#currentTrId").val();
			$("#"+lastTrId).replaceWith(trObj); 
		} else {
			listObj.append(trObj);
		}
	}
	
	function checkFiledName(opt) {
			 var name = $("#"+opt+"FiledName").val();
		     var nameMsgObj = $("#"+opt+"FiledNameMsg");
		     
		     if($.trim(name) == "") {
		     	nameMsgObj.css("color","red").html("策略字段名称必须指定");
		     	return false;
		     } else {
		     	nameMsgObj.html("");
		     	return true;
		     }
		}
		
	function checkStrategy(opt) {
		    var strategy = $("#"+opt+"Strategy option:selected").val();
			if($.trim(strategy) == "") {
				$("#"+opt+"StrategyMsg").css("color","red").html("必须指定采集策略");
				return false;
			} else if(strategy == "TAG"){
				var startTag = $("#"+opt+"StartTag").val();
				var endTag = $("#"+opt+"EndTag").val();
				
				if($.trim(startTag) == "") {
					$("#"+opt+"StartTagMsg").css("color","red").html("开始标签不能为空");
					return false;
				} else {
					$("#"+opt+"StartTagMsg").html("");
				}
				
				if($.trim(endTag) == "") {
					$("#"+opt+"EndTagMsg").css("color","red").html("结束标签不能为空");
					return false;
				} else {
					$("#"+opt+"EndTagMsg").html("");
				}
				
			} else if(strategy == "REGULAR") {
				var regular = $("#"+opt+"Regular").val();
				if($.trim(regular) == "") {
					$("#"+opt+"RegularMsg").css("color","red").html("正则表达式不能为空");
					return false;
				} else {
					$("#"+opt+"RegularMsg").html("");
				}
			} else if(strategy == "XPATH") {
			 	var xpath = $("#"+opt+"Xpath").val();
			 	if($.trim(xpath) == "") {
					$("#"+opt+"XpathMsg").css("color","red").html("Xpath不能为空");
					return false;
				} else {
					$("#"+opt+"XpathMsg").html("");
				}
			}
			$("#"+opt+"StrategyMsg").html("");
			return true;
		}
		
		function createUrlParam() {
			var url = $("#url").val();
			var urlParam = $("#urlParam");
			var position = -1;
			var htmlContent = ""; 
			var index = 0;
			while((position = url.indexOf("{*}", position+1)) != -1) {
			    htmlContent = "<div class='textRow'><div class='textLabel'>URL参数"+(index+1)+"(<span style='color:red'>*</span>)：<input type='hidden' name='urlParams["+index+"].paramIndex' value='"+index+"' class='textInfo step' size='5' maxlength='10'/></div><div class='textContent'>开始：<input type='text' name='urlParams["+index+"].start' class='textInfo step' size='5' maxlength='10'/>&nbsp;结束：<input type='text' name='urlParams["+index+"].end' class='textInfo step' size='5' maxlength='10'/>&nbsp;增减：<input type='text' name='urlParams["+index+"].step' class='textInfo step' size='5' maxlength='10'/><span id='stepMsg'></span></div></div>";
				urlParam.append(htmlContent);
				index++;
			}
		}
		
		function changeStrategy(opt) {
			var strategy = $("#"+opt+"Strategy option:selected").val();
			var tagDiv = $("#"+opt+"TagDiv");
			var regularDiv = $("#"+opt+"RegularDiv");
			var xpathDiv = $("#"+opt+"XpathDiv");
			tagDiv.hide();
			regularDiv.hide();
			xpathDiv.hide();
			if(strategy == "TAG"){
				tagDiv.show();
			} else if(strategy == "REGULAR") {
				regularDiv.show();
			} else if(strategy == "XPATH") {
				xpathDiv.show();
			}
		}
		
		function checkTask() {
			var name = $("#name").val();
			var nameMsg = $("#nameMsg");
			var url = $("#url").val();
			var urlMsg = $("#urlMsg");
			
			var nameRs = true;
			var urlRs = true;
			var urlParamRs = true;
			if($.trim(name) == "") {
				nameMsg.css("color", "red").html("任务名称必须填写");
				nameRs = false;
			} else {
				nameMsg.html("");
			}
			if($.trim(url) == "") {
				urlMsg.css("color", "red").html("采集URL必须填写");
				urlRs = false;
			} else {
				if(url.indexOf("{*}") != -1) {
				  var reg = new RegExp("^\\d+$");
				  var steps = $(".step");
				  var rs = true;
				  steps.each(function(i) {
				  	var step = $(this).val();
				  	if(!reg.test($.trim(step))) {
				  		rs = false;
				  	}
				  });
				  if(!rs) {
				  	$("#stepMsg").css("color", "red").html("url参数必须填写，且为数字");
				  	urlParamRs = false;
				  } else {
				  	$("#stepMsg").html("");
				  }
				}
			}
			return nameRs && urlRs && urlParamRs;
		}
		
		function addStrategy(level) {
			$("#levelTag").val(level);
			$("#createStrategy").dialog("open");
		}
		
		function updateStrategy(trId,level) {
			$("#levelTag").val(level);
			$("#currentTrId").val(trId);
			var currentTr = $("#"+trId);
			var filedName = currentTr.find(".tdFiledName").find("span").html();
			var strategy = currentTr.find(".tdStrategy").find("span").eq(0).html();
			var strategyValue = currentTr.find(".tdStrategyValue").find("span").eq(0).html();
			var filterTag = currentTr.find(".tdFilterTag").find("span").eq(0).html();
			
			$("#updateFiledName").val(filedName);
			
			$("#updateStrategy option[value="+strategy+"]").attr("selected",true);
			
			if(filterTag != "") {
				var filterTags = filterTag.split(",");
				
				for(var i=0; i<filterTags.length; i++) {
					var checkObj = $(".updateFilterTag[value="+filterTags[i]+"]");
					if(checkObj) {
						checkObj.attr("checked",true);
					}
				}
			}
			
			var tagDiv = $("#updateTagDiv");
			var regularDiv = $("#updateRegularDiv");
			var xpathDiv = $("#updateXpathDiv");
			tagDiv.hide();
			regularDiv.hide();
			xpathDiv.hide();
			var strategyValues = strategyValue.split(",");
			if(strategy == "TAG"){
				tagDiv.show();
				$("#updateStartTag").val(strategyValues[0]);
				$("#updateEndTag").val(strategyValues[1]);
			} else if(strategy == "REGULAR") {
				regularDiv.show();
				$("#updateRegular").val(strategyValues[0]);
			} else if(strategy == "XPATH") {
				xpathDiv.show();
				$("#updateXpath").val(strategyValues[0]);
				if(strategyValues.length==2) {
				  $("#updateXpathMode").val(strategyValues[1]);
				}
			}
			$("#updateStrategy").dialog("open");
		}
		
		function deleteStrategy(trId) {
			$("#"+trId).remove();
		}